/**
 * 
 */
package com.p2p.user.facade.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.p2p.base.util.MD5Util;
import com.p2p.front.dal.model.User;
import com.p2p.front.service.UserInfoAuthService;
import com.p2p.front.service.UserInfoService;
import com.p2p.front.service.UserService;
import com.p2p.front.vo.UserInfoVO;
import com.p2p.user.enums.UserAuthName;
import com.p2p.user.enums.UserInfoName;
import com.p2p.user.facade.UserQueryFacade;
import com.p2p.user.result.UserQueryResult;
import com.p2p.user.vo.UserInfoFullVO;

/**
 * @author ruiqi
 *
 */
@Service("userQueryFacade")
@WebService(endpointInterface="com.p2p.user.facade.UserQueryFacade",serviceName="UserQueryFacade")
public class UserQueryFacadeImpl implements UserQueryFacade{

	@Resource
	private UserInfoAuthService userInfoAuthService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private UserService userService;
	
	@Override
	public UserQueryResult getUserInfoById(String userId) {
		UserQueryResult userQueryResult = new UserQueryResult();
		
		User user = userService.findById(userId);
		if(user==null){
			userQueryResult.setResultCode(UserQueryResult.ResultCodeEnum.NoExist.name());
			return userQueryResult;
		}
		UserInfoFullVO userInfoFullVO = new UserInfoFullVO();
		userQueryResult.setUserInfoFullVO(userInfoFullVO);
		
		userInfoFullVO.setUid(user.getUid());
		userInfoFullVO.setRealname(user.getRealname());
		userInfoFullVO.setUemail(user.getUemail());
		userInfoFullVO.setUmobile(user.getUmobile());
		userInfoFullVO.setUname(user.getUname());
		userInfoFullVO.setPaypwd(user.getPaypwd());
		userInfoFullVO.setAccountNo(user.getAccountNo());
		
		
		UserInfoVO userInfoVO = userInfoService.findByNameEnums(userId,Arrays.asList(UserInfoName.values()));
		
		userInfoFullVO.setInfoMap(userInfoVO.getInfoMap());
		
		List<UserAuthName> authNameList = new ArrayList<UserAuthName>();
		authNameList.add(UserAuthName.AUTH_EMAIL);
		authNameList.add(UserAuthName.AUTH_MOBILE);
		authNameList.add(UserAuthName.AUTH_REALNAME);
		Map<Integer,Character> authMap = userInfoAuthService.getAuthByEnumList(userId, authNameList);
		
		userInfoFullVO.setAuthMap(authMap);
		
		userQueryResult.setSuccess(true);
		return userQueryResult;
	}
	
	
	/**
	 * 校验支付密码
	 * @param userId
	 * @param pwStr
	 * @return
	 */
	public UserQueryResult checkPayPwd(String userId,String pwStr) {
		UserQueryResult userQueryResult = new UserQueryResult();
		
		User user = userService.findById(userId);
		if(user==null){
			userQueryResult.setResultCode(UserQueryResult.ResultCodeEnum.NoExist.name());
			return userQueryResult;
		}
		if(StringUtils.isNotBlank(user.getPaypwd())){
			String checkPwd = MD5Util.sha1(pwStr);
			if(!StringUtils.equals(checkPwd, user.getPaypwd())){
				userQueryResult.setResultCode(UserQueryResult.ResultCodeEnum.PayPasswordError.name());
				return userQueryResult;
			}
		}
		
		userQueryResult.setSuccess(true);
		return userQueryResult;
	}

}
